package com.example.myapplication;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View {
    private int value;
    private final String TAG="MyView";
    private Paint paint=new Paint();
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        //设置图形空心
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        //设置字体大小
        paint.setTextSize(20);
        final ValueAnimator valueAnimator=ValueAnimator.ofInt(0,500);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                value=(int)animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setRepeatCount(-1);
        valueAnimator.start();
    }
    @Override
            protected void onDraw(Canvas canvas){
             super.onDraw(canvas);
             Log.e(TAG,"onDraw");
             //画直线
             //canvas.drawLine(100,100,500,500,paint);
             //画实心矩形
             //canvas.drawRect(100,100,400,400,paint);
             //圆角矩形
            // RectF rect=new RectF(100,100,400,400);
            // canvas.drawRoundRect(rect,50,50,paint);
            //圆
            //canvas.drawCircle(300,300,100,paint);
            //扇形
            //canvas.drawArc(100,100,300,300,225,90,true,paint);
            //画顶
            //canvas.drawPoint(200,200,paint);
            //路径
            //Path path=new Path();
            //path.lineTo(100,10);
            //path.lineTo(0,100);
            // path.lineTo(0,0);
            //canvas.drawPath(path,paint);
             //文字
            //canvas.drawText("MI",200,200,paint);
            //小球动画
           // canvas.drawCircle(value,value,50,paint);
           //图片移动动画
            Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.cat);
            canvas.drawBitmap(bitmap,value,value,paint);
            //canvas.drawBitmap(bitmap,200,200,paint);
           // Matrix matrix=new Matrix();
           //四个不同镜像
           /*matrix.setScale(0.5f,0.5f);
            canvas.drawBitmap(bitmap,matrix,paint);
            Matrix matrix2=new Matrix();
            matrix2.setScale(-0.5f,0.5f);
            matrix2.postTranslate(543,0);
            canvas.drawBitmap(bitmap,matrix2,paint);
            Matrix matrix3=new Matrix();
            matrix3.setScale(0.5f,-0.5f);
            matrix3.postTranslate(0,543);
            canvas.drawBitmap(bitmap,matrix3,paint);
            Matrix matrix4=new Matrix();
            matrix4.setScale(-0.5f,-0.5f);
            matrix4.postTranslate(543,543);
            canvas.drawBitmap(bitmap,matrix4,paint);*/
            /*canvas.drawBitmap(bitmap,matrix,paint);
            //图旋转不同的角度
            matrix.setRotate(90,225,225);
            canvas.drawBitmap(bitmap,matrix,paint);
            matrix.setRotate(180,225,225);
            canvas.drawBitmap(bitmap,matrix,paint);
            matrix.setRotate(270,225,225);
            canvas.drawBitmap(bitmap,matrix,paint);*/

    }
}
